﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DTO;
using BUS;

namespace QuanLyNhaSach.Employee
{
    public partial class EmployeeDetail : DevComponents.DotNetBar.Office2007Form
    {
        EmployeeBUS employeeBus;

        public ActionName Action { get; set; }
        public EmployeeDTO Employee { get; set; }
        public int EmployeeID { get; set; }

        DocSequenceBUS docSeqBUS;
        public EmployeeDetail()
        {
            InitializeComponent();
            docSeqBUS = new DocSequenceBUS();

            employeeBus = new EmployeeBUS();
            btnUpdate.Location = btnAdd.Location;
            cmbEmployeeGroup.DisplayMember = UserGroup.UsersGroupName.ToString();
            cmbEmployeeGroup.ValueMember = UserGroup.UsersGroupID.ToString();
            dtpCreateDate.Value = DateTime.Now;
        }

        private void EmployeeDetail_Load(object sender, EventArgs e)
        {
          //  txtEmployeeCode.Text = docSeqBUS.GetNextDocSequenceNumber(DocSequence.Employee.ToString());
            
            if (Action == ActionName.Insert)
            {
                DocSequenceBUS docsequenceBus = new DocSequenceBUS();
                txtEmployeeCode.Text = docsequenceBus.GetNextDocSequenceNumber(DocSequence.Employee.ToString());           
                txtEmployeeName.Focus();
                Employee = new EmployeeDTO();
                chkStatus.Checked = true;
                rdbMale.Checked = true;
            }
            else
            {
                if (EmployeeID > 0)
                {
                    Employee = employeeBus.GetEmployeeByID(EmployeeID);

                    dtpCreateDate.Value = Employee.EmployeeBeginDay;
                    txtEmployeeCode.Text = Employee.EmployeeCode;
                    txtEmployeeName.Text = Employee.EmployeeName;
                    dtpBirthday.Value = Employee.EmployeeBirthDay;
                    txtEmail.Text = Employee.EmployeeEmail;
                    txtPhone.Text = Employee.EmployeePhone;
                    txtCMND.Text = Employee.EmployeeCMND;
                    rdbMale.Checked = Employee.EmployeeSex;
                    rdbFemale.Checked = !rdbMale.Checked;
                    txtAddress.Text = Employee.EmployeeAddress;
                    chkStatus.Checked = Employee.EmployeeStatus;
                    cmbEmployeeGroup.SelectedValue = Employee.EmployeeKindCode;
                }
            }
            List<UsersGroupDTO> listUserGroupDto = new List<UsersGroupDTO>();
            listUserGroupDto = employeeBus.Load_UserGroup();
            cmbEmployeeGroup.DataSource = listUserGroupDto;
            ProcessButton();
        }



        private void btnAdd_Click(object sender, EventArgs e)
        {
            Employee.EmployeeCode = txtEmployeeCode.Text;
            Employee.EmployeeName = txtEmployeeName.Text;
            Employee.EmployeeBeginDay = dtpCreateDate.Value;
            Employee.EmployeeBirthDay = dtpBirthday.Value;
            Employee.EmployeeEmail = txtEmail.Text;
            Employee.EmployeePhone = txtPhone.Text;
            Employee.EmployeeCMND = txtCMND.Text;
            if (rdbFemale.Checked)
            {
                Employee.EmployeeSex = false;
            }
            else
                Employee.EmployeeSex = true;
            Employee.EmployeeAddress = txtAddress.Text;
            Employee.EmployeeKindCode = int.Parse(cmbEmployeeGroup.SelectedValue.ToString());
            Employee.EmployeeStatus = true;
            Employee.EmployeeNote = "";

            if (!CheckDataInput())
            {
                return;
            }

            Employee.EmployeeID = employeeBus.InsertEmployee(Employee);
            if (Employee.EmployeeID > 0)
            {
                Global.SetMessage(lblMessage, "Thêm thành công!", true);
                Action = ActionName.Update;
                ProcessButton();

                Employee.EmployeeList frmList = (Employee.EmployeeList)Application.OpenForms["EmployeeList"];
                if (frmList != null)
                {
                    frmList.EmployeeChangeAfterInsert(Employee);
                }
                //OnCategoryChanged(Category);
            }
            else
            {
                Global.SetMessage(lblMessage, "Thêm không thành công!", false);
            }
        }
        private bool CheckDataInput()
        {
            if (string.IsNullOrEmpty(txtEmployeeCode.Text) || string.IsNullOrEmpty(txtEmployeeName.Text))
            {
                Global.SetMessage(lblMessage, "Tên nhân viên không được trống!", false);
                txtEmployeeName.Focus();
                return false;
            }
            if (string.IsNullOrEmpty(dtpBirthday.Text.ToString()))
            {
                Global.SetMessage(lblMessage, "Chưa chọn ngày sinh", false);
                dtpBirthday.Focus();
                return false;
            }

            if (string.IsNullOrEmpty(txtCMND.Text))
            {
                Global.SetMessage(lblMessage, "CMND không được để trống!", false);
                txtCMND.Focus();
                return false;
            }

            return true;
        }

        public void ResetForm()
        {
            DocSequenceBUS docSeqBUS = new DocSequenceBUS();
            txtEmployeeName.Text = string.Empty;
            txtEmployeeCode.Text = docSeqBUS.GetNextDocSequenceNumber(DocSequence.Employee.ToString());
            txtAddress.Text = string.Empty;
            txtCMND.Text = string.Empty;
            dtpBirthday.Text = string.Empty;
            txtEmail.Text = string.Empty;
            txtPhone.Text = string.Empty;
            txtEmployeeName.Focus();
        }
         private void btnAddNew_Click(object sender, EventArgs e)
        {
            ResetForm();
            Action = ActionName.Insert;
            ProcessButton();
            Employee = new EmployeeDTO();
        }

        private void ProcessButton()
        {
            if (Action == ActionName.Insert)
            {
                btnUpdate.Visible = false;
                btnAdd.Visible = true;
                dtpCreateDate.Enabled = true;
            }
            else if (Action == ActionName.Update)
            {
                btnUpdate.Visible = true;
                btnAdd.Visible = false;
                dtpCreateDate.Enabled = false;
            }
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            if(!CheckDataInput())
            {
                return;
            }

            Employee.EmployeeName = txtEmployeeName.Text;
            Employee.EmployeeAddress = txtAddress.Text;
            Employee.EmployeeBirthDay = dtpBirthday.Value;
            Employee.EmployeeCMND = txtCMND.Text;
            Employee.EmployeeEmail = txtEmail.Text;
            Employee.EmployeeKindCode = Global.intParse( cmbEmployeeGroup.SelectedValue);
            Employee.EmployeePhone = txtPhone.Text;
            Employee.EmployeeSex = rdbMale.Checked;
            Employee.EmployeeStatus = chkStatus.Checked;

            if (employeeBus.UpdateEmployee(Employee))
            {
                Global.SetMessage(lblMessage, "Cập nhật thành công!", true);
                //OnCategoryChanged(Category);
                Employee.EmployeeList frmList = (Employee.EmployeeList)Application.OpenForms["EmployeeList"];
                if (frmList != null)
                {
                    frmList.EmployeeChanged(Employee);
                }
            }
            else
            {
                Global.SetMessage(lblMessage, "Cập nhật không thành công!", false);
            }
        }

 

        private void EmployeeDetail_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                switch (Action)
                {
                    case ActionName.Insert:
                        btnAdd_Click(sender, e);
                        break;
                    case ActionName.Update:
                        btnUpdate_Click(sender, e);
                        break;
                }
            }
        }

        private void txtPhone_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!char.IsNumber(e.KeyChar) && !char.IsControl(e.KeyChar))
            {
                e.Handled = true;
            }

        }

        private void txtCMND_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!char.IsNumber(e.KeyChar) && !char.IsControl(e.KeyChar))
            {
                e.Handled = true;
            }
        }

     

        private void btnExit_Click(object sender, EventArgs e)
        {
            
        }
    }
}
